﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Mapping;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class WorkAdmin : System.Web.UI.Page
    {
        private Admin _currentAdmin;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["CurrentAdmin"] != null)
                {
                    LoadAllAdsToList();
                }
            }
        }

        private void LoadAllAdsToList()
        {
            _currentAdmin = Session["CurrentAdmin"] as Admin;
            
                var allAdsFromAdmin = WorkAdDB.GetAllAdsFromAdminId(_currentAdmin.AdminId);
                if (allAdsFromAdmin.Count < 1)
                {
                    LabelNoAds.Visible = true;
                }
            ListViewAds.DataSource = allAdsFromAdmin;
            ListViewAds.DataBind();

        }

        protected void ListViewAds_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                var listViewItem = e.Item as ListViewItem;

                var workAdId = (int)ListViewAds.DataKeys[listViewItem.DataItemIndex].Value;
                WorkAdDB.DeleteWorkAd(workAdId);
                Response.Redirect("WorkAdmin.aspx");
            }
            catch (Exception ex)
            {
                //LabelException.Text = ex.Message;
            }
        }


    }
}